package com.luyuan.crm.promotion.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.Order;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.member.model.Member;
import com.luyuan.crm.member.service.MemberService;
import com.luyuan.crm.promotion.HolidaySalesService;
import com.ruijie.foundation.query.Page;
import com.ruijie.foundation.query.criteria.CriteriaTemplate;
import com.ruijie.web.jsf.action.AbstractAsynOperation;
import com.ruijie.web.jsf.action.AsynOperation;
import com.ruijie.web.jsf.util.AsynModelPanelSupport;

@Component("holidaySalesSupport")
@Scope("view")
public class HolidaySalesSupport extends AsynModelPanelSupport {
    private static final Log logger = LogFactory.getLog(HolidaySalesSupport.class);

    @Resource(name = "holidaySalesService")
    private HolidaySalesService holidaySalesService;

    @Resource(name = "memberService")
    private MemberService memberService;

    @Override
    public String getPromptMsg() {
        return "短信发送完成！";
    }

    @Override
    protected AsynOperation getOperatorInstance() {
        return new HolidayOperation();
    }

    @Override
    protected void operate(AsynOperation operator) {
        CriteriaTemplate criteriaTemplate = (CriteriaTemplate) getRequestAttribute("member.criteriaTemplate");
        String sms = (String) getRequestAttribute("holidaySales.sms");
        String remark = (String) getRequestAttribute("holidaySales.remark");
        int integral = (Integer) getRequestAttribute("holidaySales.integral");

        setRequestAttribute("asynSupport", this, true);
        operator.operate(criteriaTemplate, integral, sms, remark);
    }

    class HolidayOperation extends AbstractAsynOperation {

        private String MESSAGE_TEMPLATE = "总共{0}个会员，已经完成{1}会员积分赠送，剩余{2}个会员积分赠送！";

        private String message;

        @Override
        public String getMessages() {
            return message;
        }

        @Override
        public void asynOperate(Object... param) {
            message = "数据准备中......";

            CriteriaTemplate criteriaTemplate = (CriteriaTemplate) param[0];
            int integral = (Integer) param[1];
            String sms = (String) param[2];
            String remark = (String) param[3];

            boolean f = false;
            int p = 1;
            while (!f && getPoolEnabled()) {
                Page page = memberService.pagedQuery(criteriaTemplate, p, 100, new ArrayList<Order>());
                long totalCount = page.getTotalCount();
                long startIndex = page.getPageSize() * (page.getCurrentPageNo() - 1);
                f = !page.hasNextPage();

                @SuppressWarnings("unchecked")
                List<Member> members = (List<Member>) page.getResult();
                for (Member member : members) {
                    long remainder = totalCount - startIndex;
                    message = MESSAGE_TEMPLATE.replaceAll("\\{0\\}", String.valueOf(totalCount))
                            .replaceAll("\\{1\\}", String.valueOf((startIndex++)))
                            .replaceAll("\\{2\\}", String.valueOf(remainder));

                    try {
                        holidaySalesService.bestowIntegral(member, integral, sms, remark);
                    } catch (Throwable e) {
                        logger.error("bestowIntegral error!", e);
                    }
                }
            }

            message = "短信发送完成！";
        }
    }

}
